import { Cell } from "./cell";
// import { Joiner } from "../../utils/joiner";

/**
 * 当前操作的SKU，全部记录
 */
class SkuPending {
  pending = []; // 记录选中项
  size;

  constructor(size) {
    this.size = size;
  }

  init(sku) {
    for (let i = 0; i < sku.specs.length; i++) {
      const cell = new Cell(sku.specs[i]);
      this.insertCell(cell, i);
    }
  }

  getCurrentSpecValues() {
    const values = this.pending.map((cell) => {
      return cell ? cell.spec.value : null;
    });
    return values;
  }

  getMissingSpecKeysIndex() {
    const keysIndex = [];
    for (let i = 0; i < this.size; i++) {
      if (!this.pending[i]) {
        keysIndex.push(i);
      }
    }
    return keysIndex;
  }

  getSkuCode() {
    // const joiner = new Joiner("#");
    // this.pending.forEach((cell) => {
    //   const cellCode = cell.getCellCode();
    //   joiner.join(cellCode);
    // });
    // return joiner.getStr();
  }

  isIntact() {
    for (let i = 0; i < this.size; i++) {
      if (this._isEmptyPart(i)) {
        return false;
      }
    }
    return true;
  }

  _isEmptyPart(index) {
    return !this.pending[index];
  }

  // 插入选中Cell
  insertCell(cell, x) {
    this.pending[x] = cell;
  }
  // 移除选中Cell
  removeCell(x) {
    this.pending[x] = null;
  }
  // 根据一组fences取出指定索引cell
  findSelectedCellByX(x) {
    return this.pending[x];
  }

  isSelected(cell, x) {
    const pendingCell = this.pending[x];
    if (!pendingCell) {
      return false;
    }
    return cell.id === pendingCell.id;
  }
}

export { SkuPending };
